package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.order.domain.VerCoupon;
import com.ruoyi.service.VerCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/coupons")
public class CouponsController extends BaseController {
    @Autowired
    private VerCouponsService couponsService;

    /**
     * 根据ID查询优惠券
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getCouponById( @PathVariable Long id) {
        return AjaxResult.success(couponsService.getCouponById(id));
    }

    /**
     * 自动更新优惠券状态（定时任务调用）
     * @return
     */
    @PostMapping("/update-status")
    public AjaxResult autoUpdateCouponStatus() {
        boolean result = couponsService.autoUpdateCouponStatus();
        return AjaxResult.success(String.valueOf(result), "优惠券状态更新完成");
    }

    /**
     * 根据优惠券组ID查询优惠券
     * @param groupId
     * @return
     */
    @GetMapping("/group/{groupId}")
    public AjaxResult getCouponsByGroupId(@PathVariable Long groupId) {
        return AjaxResult.success(couponsService.getCouponsByGroupId(groupId));
    }

    /**
     * 查询所有优惠券
     * @return
     */
    @GetMapping("/available")
    public AjaxResult getAvailableCoupons(VerCoupon verCoupon) {
        return AjaxResult.success(couponsService.getAvailableCoupons(verCoupon));
    }

    /**
     * 查询指定商品可用优惠券
     * @param skuId
     * @param orderAmount
     * @return
     */
    @GetMapping("/sku/{skuId}")
    public AjaxResult getUsableCouponsForSku( @PathVariable Long skuId, @RequestParam BigDecimal orderAmount) {
        return AjaxResult.success(couponsService.getUsableCouponsForSku(skuId, orderAmount));
    }

    /**
     * 创建优惠券
     * @param coupon
     * @return
     */
    @PostMapping("/createCoupon")
    public AjaxResult createCoupon( @RequestBody VerCoupon coupon) {
        Long result=couponsService.createCoupon(coupon);
        return AjaxResult.success(String.valueOf(result), "优惠券创建成功");
    }

    /**
     * 更新优惠券
     * @param coupon
     * @return
     */
    @PutMapping("/updateCoupon/{id}")
    public AjaxResult updateCoupon(@RequestBody VerCoupon coupon) {
        boolean result = couponsService.updateCoupon(coupon);
        return AjaxResult.success(String.valueOf(result), "优惠券更新成功");
    }

    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCoupon/{id}")
    public AjaxResult deleteCoupon(@PathVariable Long id) {
        boolean result = couponsService.deleteCoupon(id);
        return AjaxResult.success(String.valueOf(result), "优惠券删除成功");
    }

    /**
     * 关联商品与优惠券
     * @param couponId
     * @param skuIds
     * @return
     */
    @PostMapping("/relate-sku/{couponId}")
    public AjaxResult relateSkuWithCoupon(@PathVariable Long couponId, @RequestBody List<Long> skuIds) {
        boolean result = couponsService.relateSkuWithCoupon(couponId, skuIds);
        return AjaxResult.success(String.valueOf(result), "商品与优惠券关联成功");
    }

    /**
     * 删除商品与优惠券关联
     * @param couponId 优惠券ID
     * @param skuIds SKU ID列表
     * @return
     */
    @PostMapping("/remove-sku-relation/{couponId}")
    public AjaxResult removeSkuCouponRelation(@PathVariable Long couponId, @RequestBody List<Long> skuIds) {
        boolean result = couponsService.removeSkuCouponRelation(couponId, skuIds);
        return AjaxResult.success(String.valueOf(result), "商品与优惠券关联删除成功");
    }

    //    /**
//     * 领取优惠券
//     * @param receiveDTO
//     * @return
//     */
//    @PostMapping("/receive")
//    public AjaxResult receiveCoupon( @RequestBody CouponReceiveDTO receiveDTO) {
//        return AjaxResult.success(couponsService.receiveCoupon(receiveDTO.getCouponId()), "优惠券领取成功");
//    }
//
//    /**
//     * 使用优惠券
//     * @param couponId
//     * @param orderId
//     * @return
//     */
//    @PostMapping("/use")
//    public Result<Boolean> useCoupon(
//            @ApiParam(value = "优惠券ID", required = true) @RequestParam Long couponId,
//            @ApiParam(value = "订单ID", required = true) @RequestParam Long orderId) {
//        return Result.success(couponsService.useCoupon(couponId, orderId), "优惠券使用成功");
//    }
}
